package com.zhj.jvm.test.ref;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;

/**
 * 当jvm内存不足时，会自动回收软引用，因此本程序不需要考虑内存溢出
 * @author zhj
 */
public class SoftCaches {
    Map<String, SoftReference<MyObject>> caches = new HashMap<>();

    void setCaches(String id, MyObject object) {
        caches.put(id, new SoftReference<>(object));
    }

    MyObject getSoftCaches(String id) {
        SoftReference<MyObject> myObjectSoftReference = caches.get(id);
        if (myObjectSoftReference == null) {
            return null;
        }
        return myObjectSoftReference.get();
    }
}
